package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.DegassingMapper;
import com.qf.myexception.ServiceException;
import com.qf.pojo.Degassing;
import com.qf.pojo.Recycle;
import com.qf.service.DegassingService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.Map;

@Service
public class DegassingServiceImpl extends ServiceImpl<DegassingMapper, Degassing> implements DegassingService  {

    @Autowired
    DegassingMapper degassingMapper;

    @Override
    public ResultMsg findAll(int current, int pageSize) {
        QueryWrapper<Degassing> wrapper = new QueryWrapper<>();
        wrapper.eq("degassing_delete",0);
        Page<Degassing> page = new Page<>(current, pageSize);
        Page<Degassing> page1 = degassingMapper.getPage(page, wrapper);
        if(page1!=null){
            return ResultMsg.SUCCESS(page1);
        }
        throw new ServiceException(201,"查询失败");
    }

    @Override
    public ResultMsg findOne(int degassingId) {
        QueryWrapper<Degassing> wrapper = new QueryWrapper<>();
        wrapper.eq("degassing_id",degassingId);
        Degassing one = degassingMapper.findOne(wrapper);
        if(one!= null)
            return ResultMsg.SUCCESS(one);
        throw new ServiceException(201,"查询失败");
    }

    @Override
    public ResultMsg findAllByDate(String startTime, String endTime, Integer current, Integer pageSize) {
        QueryWrapper<Degassing> wrapper = new QueryWrapper<>();
        wrapper.eq("degassing_delete",0);
        wrapper.between("degassing_date",startTime+" 00:00:00",endTime+" 23:59:59");
        Page<Degassing> page = new Page<>(current, pageSize);
        Page<Degassing> page1 = degassingMapper.getPage(page, wrapper);
        if(page1!=null){
            return ResultMsg.SUCCESS(page1);
        }
        throw new ServiceException(201,"查询失败");
    }

    @Override
    public ResultMsg add(Degassing degassing) {
        degassing.setDegassingDelete(0);

        boolean save = this.save(degassing);
        if(save){
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201,"新增失败");
    }

    @Override
    public ResultMsg updateDegassing(Degassing degassing) {
        boolean b = this.updateById(degassing);
        if(b){
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201,"修改失败");
    }

    @Override
    public ResultMsg delDegassing(Map<String,Object> map) {
        UpdateWrapper<Degassing> wrapper = new UpdateWrapper<>();
        wrapper.set("degassing_delete",1);
        Integer degassingId = (Integer) map.get("degassingId");
        wrapper.eq("degassing_id",degassingId);
        boolean update = this.update(wrapper);
        if(update){
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201,"删除失败");
    }
}
